package jot.jot;

import java.util.Arrays;

/**
 * 类的作用
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-11-01 14:54
 */
public class Test {


    public static void main(String[] args) {
        StringBuilder java = new StringBuilder();
        java.append("    private static boolean object(String temp) {\r\n" +
                "        if(temp.indexOf(\"{\"\")!=-1)\r\n" +
                "            return false;//不封装对象类型\r\n" +
                "        String val = temp.substring(1,temp.length()-1);\r\n" +
                "        String[] fields = val.split(\",\");\r\n" +
                "        int a = 0,b = 0,c = 0;\r\n" +
                "        for(String field : fields){\r\n" +
                "            if(field.trim().indexOf('\"')!=-1){\r\n" +
                "                a++;//字符串\r\n" +
                "            }else if(field.trim().indexOf('.')!=-1){\r\n" +
                "                b++;//精度形\r\n" +
                "            }else{\r\n" +
                "                c++;//整形\r\n" +
                "            }\r\n" +
                "        }\r\n" +
                "        //只有一种数据类型\r\n" +
                "        if((a>b?(a>c?a:c):(b>c?b:c))==fields.length){\r\n" +
                "            return false;\r\n" +
                "        }\r\n" +
                "        //含有多种数据类型\r\n" +
                "        return true;\r\n" +
                "    }");

        System.out.println(java.toString());

        int endMethodIndex = findEndMethod(java.toString(), indexOf(java.toString(),0,java.toString().length(),"{")+1, java.length());
        System.out.println("下标为 = 》"+endMethodIndex);

    }

    /**
     * 从方法的{下标+1开始找，找到该方法的}下标，结束方法
     * @param xml
     * @param i
     * @param j
     * @return
     */
    public static int findEndMethod(String xml,int i ,int j){
        int left = 1;
        for(;i<j;i++){
            if(xml.charAt(i)=='\''){
                int temp = i + 1;
                while (temp<j&&(temp = indexOf(xml,temp,j,"\'"))!=-1){
                    System.out.println("=='=="+temp);
                    if(xml.charAt(temp-1)!='\\'){
                        break;
                    }
                    temp++;
                }
                if(temp==-1){
                    throw new RuntimeException("没有匹配到对应的单引号！");
                }
                i = temp + 1;
            }
            if(xml.charAt(i)=='"'){
                int temp = i + 1;
                while (temp<j&&(temp = indexOf(xml,temp,j,"\""))!=-1){
                    System.out.println("==\"=="+temp);
                    if(xml.charAt(temp-1)!='\\'){
                        break;
                    }
                    temp++;
                }
                if(temp==-1){
                    throw new RuntimeException("没有匹配到对应的双引号！");
                }
                i = temp + 1;
            }
            if(xml.charAt(i)=='{'){
                left++;
            }
            if(xml.charAt(i)=='}'){
                left--;
                System.out.println("left ==>"+left);
                if(left==0){
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * 从xml文本内容，从i下标 开始找到findvalue字符串，返回该字符的下标
     * @param xml
     * @param i
     * @param findValue
     * @return
     */
    public static int indexOf(String xml,int i,int end,String findValue){
        //获取kmp码（下标）
        int[] kmp = nextKMP(findValue);
        int len = 0;
        for(;i<end;i++){
            //找到//的话，直接找到\r\n再继续读取
            if(xml.charAt(i)=='/'&&i+1<xml.length()&&xml.charAt(i+1)=='/'){
                i = find('\r','\n',xml,i);
                if(i==-1){
                    System.out.println("找到//注释，但没有找到结束换行标识，说明//是最后一行");
                    i = xml.length();
                }
            }
            //扎到/*的话，直接找到*/再继续读取
            if(xml.charAt(i)=='/'&&i+1<xml.length()&&xml.charAt(i+1)=='*') {
                i = find('*','/',xml,i);
                if(i==-1){
                    throw new RuntimeException("只找到了/*注释，但没有找到结束标识：*/");
                }
            }
            //kmp算法，避免频繁的回溯
            while(len>0&&xml.charAt(i)!=findValue.charAt(len)){
                len = kmp[len - 1];
            }
            if(xml.charAt(i)==findValue.charAt(len)){
                len++;
            }
            if(len==findValue.length()){
                //找到findValue
                return i - len + 1;
            }

        }
        return -1;
    }

    /**
     * 计算需要查找字符串的KMP下标
     * @param value
     * @return
     */
    public static int[] nextKMP(String value){
        int[] kmp = new int[value.length()];
        int j = 0;
        for(int i = 1;i<value.length();i++){
            while(j>0&&value.charAt(i)!=value.charAt(j)){
                j--;
            }
            if(value.charAt(i)==value.charAt(j)){
                j++;
            }
            kmp[i] = j;
        }
        return kmp;
    }


    /**
     * 找到// 或者 /* 注释
     * @param a
     * @param b
     * @param xml
     * @param i
     * @return
     */
    public static int find(char a,char b,String xml,int i){

        int j = i+2;
        while(j<xml.length()) {
            if (xml.charAt(j) == a && j + 1 < xml.length() && xml.charAt(j + 1) == b) {
//                System.out.println("注释 ：" + xml.substring(i, j+2));
                return j + 1;
            }
            j++;
        }
        return -1;
    }


}
